package br.com.catalogodigital.bean;


import java.io.IOException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.catalogodigital.dao.AnuncianteDao;
import br.com.catalogodigital.dao.AnuncioDao;
import br.com.catalogodigital.dao.ServicoDao;
import br.com.catalogodigital.entity.Anunciante;
import br.com.catalogodigital.entity.Servico;




@ManagedBean
@RequestScoped
public class ServicoBean {

	
	private Servico servico = new Servico();
	private ServicoDao servicoDao = new ServicoDao();
	private Anunciante anunciante = new Anunciante();
	private AnuncianteDao anuncianteDao = new AnuncianteDao();
	private List<Servico> servicos;
	
	
	
	public void alterarServico() throws IOException{
		
		this.servico = servicos.get(0);
		
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpSession session = (HttpSession) ec.getSession(false);
		
		this.servico.setId( (Integer) session.getAttribute("idServico")); 
		
		if (servicoDao.buscarId(this.servico.getId()) != null) {

			
			ServicoDao servicoDao = new ServicoDao();
			servicoDao.alterar(servico);

			ec.redirect("config.xhtml");
			
		} 
		
		}
	
	
	
	public ServicoBean() {
		super();
		// TODO Auto-generated constructor stub
	}


	public ServicoBean(Servico servico, ServicoDao servicoDao,
			Anunciante anunciante, AnuncianteDao anuncianteDao) {
		super();
		this.servico = servico;
		this.servicoDao = servicoDao;
		this.anunciante = anunciante;
		this.anuncianteDao = anuncianteDao;
	}
	public Servico getServico() {
		return servico;
	}
	public void setServico(Servico servico) {
		this.servico = servico;
	}
	public ServicoDao getServicoDao() {
		return servicoDao;
	}
	public void setServicoDao(ServicoDao servicoDao) {
		this.servicoDao = servicoDao;
	}
	public Anunciante getAnunciante() {
		return anunciante;
	}
	public void setAnunciante(Anunciante anunciante) {
		this.anunciante = anunciante;
	}
	public AnuncianteDao getAnuncianteDao() {
		return anuncianteDao;
	}
	public void setAnuncianteDao(AnuncianteDao anuncianteDao) {
		this.anuncianteDao = anuncianteDao;
	}



	public List<Servico> getServicos() {
		
		if (this.servicos == null) {
			ServicoDao servicoDao = new ServicoDao();
			this.servicos = servicoDao.listar();
			
			FacesContext fc = FacesContext.getCurrentInstance();
			ExternalContext ec = fc.getExternalContext();
			HttpSession session = (HttpSession) ec.getSession(false);
			
			session.setAttribute("idServico", this.servicos.get(0).getId());
			
		}
		return servicos;
	}



	public void setServicos(List<Servico> servicos) {
		this.servicos = servicos;
	}
	

	
	
	
	
}




